-- 战斗飘字管理
local FightPromptText = class("FightPromptText")
local GameUtils = require("utils.GameUtils")
local Vector3 = require("utils.Vector3")
local Vector2 = require("utils.Vector2")

function FightPromptText:ctor()
    self._flyQueue = {}
end

function FightPromptText:awake()
    self._promptUI = G_UIManager:appendPromptUI()
    self._promptRect = self._promptUI:GetComponent(typeof(CS.UnityEngine.RectTransform))
    self._promptRect.localPosition = Vector3.new(0, 0, 0)
    self._promptRect.localScale = Vector3.new(1, 1, 1)
    self._promptRect.sizeDelta = Vector2.new(0, 0)
    self._signalEventFightHurtText =
        G_SignalManager:add(SignalConst.EVENT_FIGHT_HURT_TEXT, handler(self, self._onEventFightHurtText))
    self._signalEventDestroyHurt =
        G_SignalManager:add(SignalConst.EVENT_DESTROY_HURT_TEXT, handler(self, self._onEventDestoryHurtText))
end

function FightPromptText:start()
    self._promptUI.transform:SetSiblingIndex(99)
end

function FightPromptText:onDestroy()
    self._signalEventFightHurtText:remove()
    self._signalEventFightHurtText = nil
    self._signalEventDestroyHurt:remove()
    self._signalEventDestroyHurt = nil
    GameUtils.destroy(self._promptUI)
end

function FightPromptText:_onEventFightHurtText(id, params)
    local hurtFly = self:_createHurtFly(params)
    if not self._flyQueue[params.parent] then
        self._flyQueue[params.parent] = {}
    end
    table.insert(self._flyQueue[params.parent], hurtFly)
    self:_layoutFly(self._flyQueue[params.parent])
end

function FightPromptText:_layoutFly(list)
    local startPos = list[1]:getPos()
    local size = #list
    for i = 1, size do
        local posY = startPos.y + (size - i) * 28
        local rect = list[i]:getRect()
        rect.localPosition = Vector3.new(startPos.x, posY, startPos.z)
    end
end

function FightPromptText:_createHurtFly(params)
    local hurtFly = require("gameLogic.ui.HurtFly"):create(self, params.parent)
    hurtFly:setHurtText(params)
    GameUtils.addChildTo(self._promptUI.transform, hurtFly.transform)
    local localPos = GameUtils.convertWorldPosToUIPos(params.position, self._promptRect)
    local rect = hurtFly.gameObject:GetComponent(typeof(CS.UnityEngine.RectTransform))
    rect.localScale = Vector3.new(1, 1, 1)
    rect.localEulerAngles = Vector3.new(0, 0, 0)
    hurtFly:setPos(Vector3.new(localPos.x, localPos.y + 160, -10))
    hurtFly:setRect(rect)
    return hurtFly
end

function FightPromptText:onHurtFlyDieNotify(hurtFly)
    local list = self._flyQueue[hurtFly:getParent()]
    for i = 1, #list do
        if list[i] == hurtFly then
            table.remove(list, i)
        end
    end
end

return FightPromptText
